<template>
    <div class="gaode-map-container">
      <div ref="mapRef" class="map-dom" style="width: 100%; height: 100%;"></div>
      <!-- 设备状态面板 -->
      <div class="device-panel">
        <div class="panel-title">当前位置：番禺区</div>
        <div class="panel-item">
          <i class="icon normal"></i>
          <span>正常使用</span>
          <span class="count">设备数量：10</span>
        </div>
        <div class="panel-item">
          <i class="icon fault"></i>
          <span>出现故障</span>
          <span class="count">设备数量：3</span>
        </div>
        <div class="panel-item">
          <i class="icon idle"></i>
          <span>空闲中</span>
          <span class="count">设备数量：20</span>
        </div>
      </div>
      <!-- 业务量面板 -->
      <div class="business-panel">
        <div class="business-item">
          <div class="business-label">A业务量</div>
          <div class="business-value">300</div>
        </div>
        <div class="business-item">
          <div class="business-label">B业务量</div>
          <div class="business-value">300</div>
        </div>
        <div class="business-item">
          <div class="business-label">C业务量</div>
          <div class="business-value">300</div>
        </div>
        <div class="business-item">
          <div class="business-label">D业务量</div>
          <div class="business-value">300</div>
        </div>
      </div>
    </div>
  </template>
  
  <script lang="ts">
  import { defineComponent, ref, onMounted, onUnmounted } from 'vue';
  
  export default defineComponent({
    name: 'GaodeMapWithMarkers',
    setup() {
      const mapRef = ref<HTMLDivElement | null>(null);
      let map: any = null; // 高德地图实例
  
      onMounted(() => {
        // 加载高德地图 SDK（使用稳定版本 2.0，替换为你的 API Key）
        const script = document.createElement('script');
        script.src = 'https://webapi.amap.com/maps?v=2.0&key=20b79de044b8d1dfeb8156989f3bbd9a'; // 版本改为2.0
        script.onload = initMap;
        document.body.appendChild(script);
      });
  
      const initMap = () => {
        if (!mapRef.value || !(window as any).AMap) return;
  
        // 初始化地图
        map = new (window as any).AMap.Map(mapRef.value, {
          zoom: 13,
          center: [113.39, 22.91], // 番禺区中心点坐标
          mapStyle: 'amap://styles/darkblue', // 内置深色主题
        });
  
        // 添加设备标记
        const markers = [
          // 正常设备（蓝色）
          { position: [113.36, 22.91], type: 'normal' },
          { position: [113.40, 22.86], type: 'normal' },
          { position: [113.32, 22.82], type: 'normal' },
          // 故障设备（红色，闪烁动画）
          { position: [113.42, 22.95], type: 'fault' },
          { position: [113.35, 22.90], type: 'fault' },
          // 空闲设备（绿色）
          { position: [113.45, 22.82], type: 'idle' },
          { position: [113.28, 22.80], type: 'idle' },
        ];
  
        markers.forEach((marker) => {
          const icon = new (window as any).AMap.Icon({
            size: new (window as any).AMap.Size(24, 24),
            image: getMarkerImage(marker.type),
            imageSize: new (window as any).AMap.Size(24, 24),
          });
  
          const mapMarker = new (window as any).AMap.Marker({
            position: marker.position,
            icon,
            zIndex: 10,
          });
  
          map.add(mapMarker);
  
          // 故障设备添加闪烁动画
          if (marker.type === 'fault') {
            let opacity = 1;
            setInterval(() => {
              opacity = opacity === 1 ? 0.3 : 1;
              mapMarker.setOpacity(opacity);
            }, 800);
          }
        });
      };
  
      // 获取标记图标（可替换为实际图标路径）
      const getMarkerImage = (type: string) => {
        switch (type) {
          case 'normal':
            return 'https://cdn8.axureshop.com/demo2025/2328232/images/01__%E6%94%BF%E5%8A%A1%E6%95%B0%E6%8D%AE%E7%9B%91%E6%8E%A7%E5%A4%A7%E5%B1%8F/u820.svg';
          case 'fault':
            return 'https://cdn8.axureshop.com/demo2025/2328232/images/01__%E6%94%BF%E5%8A%A1%E6%95%B0%E6%8D%AE%E7%9B%91%E6%8E%A7%E5%A4%A7%E5%B1%8F/u849.svg';
          case 'idle':
            return 'https://cdn8.axureshop.com/demo2025/2328232/images/01__%E6%94%BF%E5%8A%A1%E6%95%B0%E6%8D%AE%E7%9B%91%E6%8E%A7%E5%A4%A7%E5%B1%8F/u840.svg';
          default:
            return '';
        }
      };
  
      onUnmounted(() => {
        if (map) {
          map.destroy();
        }
      });
  
      return {
        mapRef,
      };
    },
  });
  </script>
  
  <style scoped>
  /* 样式保持不变 */
  .gaode-map-container {
    position: relative;
    width: 98%;
    height: 98%;
    margin: 0 auto;
    padding-top: 10px;
  }
  
  .map-dom {
    width: 100%;
    height: 100%;
    
  }
  
  .device-panel {
    position: absolute;
    top: 20px;
    left: 20px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    padding: 10px;
    border-radius: 4px;
    z-index: 100;
  }
  
  .panel-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
  }
  
  .panel-item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
  }
  
  .icon {
    display: inline-block;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    margin-right: 8px;
  }
  
  .normal {
    background-color: #4da7f5;
  }
  
  .fault {
    background-color: #ff3333;
  }
  
  .idle {
    background-color: #4df57a;
  }
  
  .count {
    margin-left: 8px;
    opacity: 0.8;
  }
  
  .business-panel {
    position: absolute;
    top: 20px;
    right: 20px;
    display: flex;
    flex-direction: column;
    z-index: 100;
  }
  
  .business-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: linear-gradient(135deg, #00f2ff, #0575e6);
    margin-bottom: 10px;
    color: #fff;
    justify-content: center;
  }
  
  .business-label {
    font-size: 12px;
    margin-bottom: 4px;
  }
  
  .business-value {
    font-size: 16px;
    font-weight: bold;
  }
  </style>